********************************
** Read in file, add controls **

use 	"./output/cities_expandedpatents", clear

merge 1:1 gen_id f_myr using "./output/cities_prohibition.dta"
keep if _merge==3
drop _merge

** npi X month-of-year FEs **
egen	idxm = group(gen_id f_m)

** census region X year FEs **
egen	regyr = group(f_yr region)

*=======================*
*= Treatment variables  *
*=======================*

tab 	days_npis if f_myr==tm(1918m9)

gen		daysnpis = days_npis/30 
gen		lnnpis = ln(days_npis)
gen		longnpis = (days_npis>=90) if !mi(days_npis) // Natural break in data, about 61% have short
gen		ldeaths = ln(mag_maxexcessdeaths)

foreach v of varlist daysnpis longnpis lnnpis ldeaths {
	gen 	post_X_`v' 		= `v'*(f_myr>=tm(1918m9))

	gen 	during_X_`v' 	= `v'*( (f_myr>=tm(1918m9)) & (f_myr<=tm(1919m3)) )
	gen 	after_X_`v' 	= `v'*(f_myr>=tm(1919m4))
	gen 	pre_X_`v' 		= `v'*(f_myr<=tm(1917m8))
	
	lab var post_X_`v'		"Post Pandemic $\times$ NPI Length"
	
	lab var pre_X_`v'		"Before Pandemic $\times$ NPI Length"
	lab var during_X_`v' 	"During Pandemic $\times$ NPI Length"
	lab var after_X_`v' 	"After Pandemic $\times$ NPI Length"
}

lab var proh_18th		"Alcohol Prohibited (18th Amend.)"
lab var proh_wpa		"Alcohol Prohibited (WPA)"

*=======================*
*= Regression Models =*
*=======================*

foreach v in longnpis {
	
	local treat post_X_`v'

	eststo: ppmlhdfe pat_wtd_inv `treat' proh_18th if tin(1916m1, 1920m12), exp(pop_int) a(f_myr gen_id) cluster(gen_id)
	eststo: ppmlhdfe pat_wtd_inv `treat' proh_wpa if tin(1916m1, 1920m12), exp(pop_int) a(f_myr gen_id) cluster(gen_id)
	
	eststo: ppmlhdfe pat_wtd_inv `treat' proh_18th if tin(1916m1, 1920m12), exp(pop_int) a(f_myr idxm) cluster(gen_id)
	eststo: ppmlhdfe pat_wtd_inv `treat' proh_wpa if tin(1916m1, 1920m12), exp(pop_int) a(f_myr idxm) cluster(gen_id)
	
	eststo: ppmlhdfe pat_wtd_inv `treat' proh_18th if tin(1916m1, 1920m12), exp(pop_int) a(f_myr idxm regyr) cluster(gen_id)
	eststo: ppmlhdfe pat_wtd_inv `treat' proh_wpa if tin(1916m1, 1920m12), exp(pop_int) a(f_myr idxm regyr) cluster(gen_id)
	
	estout 	using "$RES/fortable/prohibition_posteffect_`v'.tex", replace cells(b(star fmt(%9.3f)) se(par)) ///
		stats(N, fmt(%9.0g) labels("\(N\)"))  starlevels(+ 0.10 * 0.05 ** 0.01 *** 0.001) ///
		style(tex) drop(_cons) eqlabels(none) label 
	est 	clear
}

foreach v in longnpis  {
	
	local treat pre_X_`v' during_X_`v' after_X_`v'

	eststo: ppmlhdfe pat_wtd_inv `treat' proh_18th if tin(1916m1, 1920m12), exp(pop_int) a(f_myr gen_id) cluster(gen_id)
	eststo: ppmlhdfe pat_wtd_inv `treat' proh_wpa if tin(1916m1, 1920m12), exp(pop_int) a(f_myr gen_id) cluster(gen_id)
	
	eststo: ppmlhdfe pat_wtd_inv `treat' proh_18th if tin(1916m1, 1920m12), exp(pop_int) a(f_myr idxm) cluster(gen_id)
	eststo: ppmlhdfe pat_wtd_inv `treat' proh_wpa if tin(1916m1, 1920m12), exp(pop_int) a(f_myr idxm) cluster(gen_id)
	
	eststo: ppmlhdfe pat_wtd_inv `treat' proh_18th if tin(1916m1, 1920m12), exp(pop_int) a(f_myr idxm regyr) cluster(gen_id)
	eststo: ppmlhdfe pat_wtd_inv `treat' proh_wpa if tin(1916m1, 1920m12), exp(pop_int) a(f_myr idxm regyr) cluster(gen_id)

	estout 	using "$RES/fortable/prohibition_duringafterpre_`v'.tex", replace cells(b(star fmt(%9.3f)) se(par)) ///
		stats(N, fmt(%9.0g) labels("\(N\)"))  starlevels(+ 0.10 * 0.05 ** 0.01 *** 0.001) ///
		style(tex) drop(_cons) eqlabels(none) label 
	est 	clear
}

